﻿@{
    ViewBag.Title = "Form";
    Layout = "~/Views/Shared/_ManufactureList.cshtml";
}
<style type="text/css">
    .ui-jqgrid .ui-jqgrid-pager .ui-pager-table,
    .ui-jqgrid .ui-jqgrid-toppager .ui-pager-table {
        width: 95%;
    }
</style>
<script type="text/javascript">
    var currentList = @Html.Raw(ViewBag.companys)
    $(function () {
        initControl();
        gridList();
    });
    function initControl() {
        //$('select').bindSelect();
        var numbers = $.request("numbers");
        $('#FHappComNOList').val(numbers);
        var names = $.request("names");
        $('#FDesc').val(names);
    }
    function gridList() {
        var $gridList = $("#gridList");
        $gridList.dataGrid({
            url: "/BOM/Manufacture/GetSupplierJson",
            height: 240,
            colModel: [
                {
                    label: '选中', name: 'UnitNO', width: 40, align: 'center', sortable: false, formatter: function (cellvalue, options, rowObject) {
                        var index = findElement(currentList, "UnitNO", rowObject.UnitNO);
                        var chb_status = '';
                        if (index >= 0) {
                            chb_status = 'checked="checked"';
                        }
                        return '<input id="chb_' + cellvalue + '" value="' + options.rowId + '" name="checkbox" ' + chb_status + ' type="checkbox" onclick="selectCurr(this)" style="vertical-align:middle">';
                    }
                },
                { label: '单位编码', name: 'UnitNO', width: 70, align: 'center', sortable: false },
                { label: '单位名称', name: 'UnitShortName', width: 120, align: 'left', sortable: false }
            ],
            pager: "#gridPager",
            sortname: 'UnitNO asc',
            viewrecords: true,
            rownumWidth: 40,
            ondblClickRow: function (rowid) {

            },
            gridComplete: function () {

            },
            loadComplete: function () {

            },
            onSelectRow: function (rowid) {

            },
            onPaging: function (page_btn) {

            }
        });
        $gridList.jqGrid('setLabel', 0, '序号', 'rowNumberHeaderStyle');
        $("#btn_search").click(function () {
            $gridList.jqGrid('setGridParam', {
                postData: { keyword: $("#txt_keyword").val() },
            }).trigger('reloadGrid');
        });
    }
    function selectCurr(obj) {
        var rowid = $(obj).val();
        var $gridList = $("#gridList");
        var rowData = $gridList.jqGrid('getRowData', rowid);
        var item = { UnitNO: rowData.UnitNO, UnitShortName: rowData.UnitShortName };
        var index = findElement(currentList, "UnitNO", rowData.UnitNO);
        if ($(obj).prop('checked')) {
            if (index < 0) {
                if (currentList.length >= 5) {
                    $.modalAlert("选中的发交单位已经达到五个，不能再选。", 'warning');
                    $(obj).prop("checked", false);
                } else {
                    currentList.push(item);
                }
            }
        } else {
            if (index >= 0) {
                currentList.splice(index, 1);
            }
        }
        SetCompany(currentList);
    }
    function SetCompany(list) {
        var numbers = [];
        var names = [];
        for (var i = 0; i < list.length; i++) {
            numbers.push(list[i].UnitNO);
            names.push(list[i].UnitShortName);
        }
        $("#FHappComNOList").val(numbers.join(','));
        $("#FDesc").val(names.join(','));
    }
    function findElement(arrayToSearch, attr, val) {
        for (var i = 0; i < arrayToSearch.length; i++) {
            if (arrayToSearch[i][attr] == val) {
                return i;
            }
        }
        return -1;
    }
    function btn_ok() {
        if (currentList.length > 5) {
            $.modalAlert("选中的发交单位已经超过五个，请修改。", 'warning');
            $(obj).prop("checked", false);
            return;
        }
        var father = $.currentSubWindow().frames["Detail"];
        father.SetCompany(currentList);
        father.$.detailCloseTop();
    }
    function btn_cancel() {
        var father = $.currentSubWindow().frames["Detail"];
        father.$.detailCloseTop();
    }
</script>
<div id="layout" style="height: 100%; width: 100%; ">
    <div style="padding: 12px 0 5px 0; padding-left: 15px; border-bottom: 1px solid #ccc;" class="line-table input-width-185">
        <label class="prefix">单位编码</label> <input id="txt_keyword" type="text" class="form-control" />
        <div class="btn-group" style="">
            <a id="btn_search" class="btn btn-primary center-block btn-1" onclick=""><span class="glyphicon glyphicon-search"></span> 检索</a>
        </div>
    </div>
    <div>
        <table id="gridList"></table>
        <div id="gridPager"></div>
    </div>
    <table class="form" style="margin-top:10px;">
        <tr>
            <th class="formTitle">发交单位</th>
            <td class="formValue input-width-740" colspan="3">
                <input id="FHappComNOList" name="FHappComNOList" type="text" class="form-control" readonly="readonly" />
            </td>
        </tr>
        <tr>
            <th class="formTitle">单位名称</th>
            <td class="formValue input-width-740" colspan="3">
                <input id="FDesc" name="FDesc" type="text" class="form-control" readonly="readonly" />
            </td>
        </tr>
    </table>
    <div style="text-align:right;padding:7px 40px 0 0;">
        <div class="btn-group" style="">
            <a id="btn_ok" class="btn btn-primary center-block" onclick="btn_ok()"><span class="glyphicon glyphicon-ok"></span> 确定</a>
        </div>
        <div class="btn-group" style="margin-left:30px;">
            <a id="btn_cancel" class="btn btn-danger center-block" onclick="btn_cancel()"><span class="glyphicon glyphicon-remove"></span> 关闭</a>
        </div>
    </div>
</div>

